package com.ny.controller;

import com.ny.entity.Result;
import com.ny.entity.User;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    
    // json测试：http://127.0.0.1:8081/register  POST
    // @RequestBody的意思：表示将请求体中的JSON数据映射为User对象
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        User savedUser = userService.addUser(user);
        if (savedUser != null) {
            return Result.success("注册成功");
        }
        return Result.error("注册失败");
    }
    
    // 更新用户信息接口
    @PutMapping("/user")
    public Result updateUser(@RequestBody User user) {
        User updatedUser = userService.updateUser(user);
        if (updatedUser != null) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }
    
    // 获取所有用户
    @GetMapping("/users")
    public Result findAllUsers() {
        List<User> users = userService.findAll();
        return Result.success(users);
    }
}